import {
	wechatSetting,
	userInfo
} from "@/request/api";
import wx from 'weixin-js-sdk';
export default function() {
	const isWechat = () => {
		var ua = window.navigator.userAgent.toLowerCase();
		if (ua.match(/micromessenger/i) != 'micromessenger') {
			uni.$uv.toast('请在微信浏览器中打开');
			return false;
		}
		return true;
	};
	const setConfig = async () => {
		let url = window.location.href.split('#')[0];
		let {
			debug,
			appId,
			timestamp,
			nonceStr,
			signature,
			jsApiList
		} = await wechatSetting({
			url
		});
		wx.config({
			debug,
			appId,
			timestamp,
			nonceStr,
			signature,
			jsApiList
		});
	};
	const wxPay = (data, success) => {
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',
			data,
			(res) => {
				if (res.err_msg == "get_brand_wcpay_request:ok") {
					setTimeout(() => {
						success()
					}, 1500);
				}
			});
	};
	const wxShare = async (data) => {
		wx.updateAppMessageShareData({
			...data,
			success: function() {}
		})
	};
	const wxScanQRCode = async (success) => {
		wx.scanQRCode({
			needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
			scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
			success: res => success(res.resultStr)
		});
	};
	const wxOpenLocation = async (res = {
		latitude: 0,
		longitude: 0,
		name: '',
		address: '',
		scale: 1,
		infoUrl: ''
	}) => {
		wx.openLocation({
			latitude: parseFloat(res.latitude), // 纬度，浮点数，范围为90 ~ -90
			longitude: parseFloat(res.longitude), // 经度，浮点数，范围为180 ~ -180。
			name: res.name, // 位置名
			address: res.address, // 地址详情说明
			scale: 15, // 地图缩放级别,整型值,范围从1~28。默认为最大
			infoUrl: res.infoUrl // 在查看位置界面底部显示的超链接,可点击跳转
		});
	};
	const wxGetLocation = async (success) => {
		wx.getLocation({
			type: 'wgs84',
			success: res => success(res)
		});
	};
	return {
		setConfig,
		wxPay,
		wxShare,
		wxScanQRCode,
		wxOpenLocation,
		wxGetLocation
	}
}